﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKArchMethodLibrary.Utils;
using YJKTextDimension.CommandSetUtility;
using YJKView;

namespace YJKTextDimension.Commands
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdLabelLengthAlign : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)111;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			View activeView = cmdData.Application.ActiveUIDocument.ActiveView;
			if (!ViewHandle.ViewValidating(cmdData.Application.ActiveUIDocument.Document, activeView, ref message))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			Transaction transaction = new Transaction(this.m_doc, "LabelLengthAlign");
			Result result;
			for (;;)
			{
				transaction.Start();
				DimensionFilter dimensionFilter = new DimensionFilter();
				IList<Element> list = null;
				XYZ ptAlign = null;
				try
				{
					while (list == null || list.Count == 0)
					{
						list = this.m_selection.PickElementsByRectangle(dimensionFilter, "请框选所有要对齐的尺寸");
					}
					ptAlign = this.m_selection.PickPoint("请点取对齐点位置：");
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				catch (Exception ex)
				{
					string message2 = ex.Message;
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				try
				{
					this.LabelLengthAlign(list, ptAlign);
				}
				catch (Exception ex2)
				{
					string message3 = ex2.Message;
					transaction.RollBack();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				transaction.Commit();
			}
			return result;
		}

		private void LabelLengthAlign(IList<Element> arElement, XYZ ptAlign)
		{
			foreach (Element element in arElement)
			{
				Dimension dimension = element as Dimension;
				Line dimensionLine = this.GetDimensionLine(dimension);
				double num = dimensionLine.Distance(ptAlign);
				double num2 = num / (double)this.m_doc.ActiveView.Scale;
				string str = string.Format("HY_尺寸界线长度{0:F2}_", num);
				string text = str + dimension.DimensionType.Name;
				List<string> list = new List<string>();
				List<DimensionType> dimensionTypes = Common.GetDimensionTypes(this.m_doc);
				foreach (DimensionType dimensionType in dimensionTypes)
				{
					list.Add(dimensionType.Name);
				}
				int num3 = list.IndexOf(text);
				DimensionType dimensionType2;
				if (num3 < 0)
				{
					dimensionType2 = (dimension.DimensionType.Duplicate(text) as DimensionType);
					if (dimensionType2.get_Parameter(BuiltInParameter.DIM_WITNS_LINE_CNTRL).AsInteger() == 0)
					{
						dimensionType2.get_Parameter(BuiltInParameter.DIM_WITNS_LINE_CNTRL).Set(1);
					}
					dimensionType2.get_Parameter(BuiltInParameter.DIM_WITNS_LINE_EXTENSION_BELOW).Set(num2);
				}
				else
				{
					dimensionType2 = dimensionTypes[num3];
				}
				dimension.DimensionType = dimensionType2;
			}
		}

		private Line GetDimensionLine(Dimension dim)
		{
			double num = dim.get_Parameter(BuiltInParameter.DIM_VALUE_LENGTH).AsDouble();
			Curve curve = dim.Curve;
			Line line = curve as Line;
			XYZ origin = line.Origin;
			XYZ direction = line.Direction;
			XYZ xyz = origin - direction * num / 2.0;
			XYZ xyz2 = origin + direction * num / 2.0;
			Line line2 = Line.CreateBound(xyz, xyz2);
			line2.MakeUnbound();
			return line2;
		}

		private Document m_doc;

		private Selection m_selection;
	}
}
